<?php
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH.'libraries/ApiController.php';
class Article extends ApiController
{
	public function __construct()
	{
	    parent::__construct();
	}

	function index(){

	}

	public function list(){
		$token=123;//$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			  ["catid",False,"分类id"],
			  ['title',False,"文章标题",""],
			  ["intro",False,"分类简介",""],
			],'post');
			
			$this->db->like("title",$data["title"]);
			$list=$this->db->select("count(*) as count")->get("article")->row_array();
			
			$this->db->like("article.title",$data["title"]);
			$this->db->select("articleid,article.title, article_category.title as category,article.catid,author,intro,article.createtime");
			$list=$this->db->join("article_category",'article_category.catid=article.catid','left')->get("article")->result_array();
			
			 
			$this->WRITE("code",1);
			$this->WRITE("list",$list);
			$this->WRITE("msg","success");
			$this->RESPONSE();
		}
	}
	
	public function list_by_page(){
		$token=123;//$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			  ["catid",False,"分类id"],
			  ['title',False,"文章标题",""],
			  ["intro",False,"分类简介",""],
			],'post');
			
			$this->db->like("title",$data["title"]);
			$list=$this->db->select("count(*) as count")->get("article")->row_array();
			
			$this->db->like("title",$data["title"]);
			$list=$this->db->select("*")->get("article")->result_array();
			
			 
 			$this->WRITE("code",1);
			$this->WRITE("list",$list);
			$this->WRITE("msg","success");
			$this->RESPONSE();
		}
	}
	
	//新增分类时，获取下拉表里的分类
	public function list_array(){
		$token=$this->verifyToken();
		if($token){
			$list=$user=$this->db->select("*")->get("article")->result_array();
    		$mlist=$this->buildList($list,-1);
			$this->WRITE("code",1);
			$this->WRITE("list",$mlist);
			$this->WRITE("msg","success");
			$this->RESPONSE();
		}
	}
	
	public function create(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			  ['catid',True,"文章分类"],
			  ['title',True,"文章标题"],
			  ["author",False,"作者",""],
			  ["content",False,"内容主题",""]
			],'post');
			
			$res=$this->db->insert("article",$data);
			if($res){
				$this->WRITE("code",1);
				$this->WRITE("msg","success");
				$this->RESPONSE();
			}else{
				$this->WRITE("code",1);
				$this->WRITE("msg","添加分类失败");
				$this->RESPONSE();
			}
		}
	}
	
	public function detail(){

			$token=$this->verifyToken();
			if($token){
				$data=$this->getKeys([["articleid",True,"文章id"]],'post');
				$row=$this->db->where("articleid",$data["articleid"])->get("article")->row_array();
				$this->WRITE("code",1);
				$this->WRITE("detail",$row);
				$this->WRITE("msg","编辑分类成功!");
				$this->RESPONSE();
			}

	}

	
	public function update(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			  ['articleid',True,"文章ID"],
			  ['catid',True,"文章分类"],
			  ['title',True,"文章标题"],
			  ["author",False,"作者",""],
			  ["content",False,"内容主题",""]
			],'post');
			
			$articleid=$data["articleid"];
			unset($data["articleid"]);
			$res=$this->db->where("articleid",$articleid)->update("article",$data);
			if($res){
				$this->WRITE("code",1);
				$this->WRITE("msg","编辑文章成功");
				$this->RESPONSE();
			}else{
				$this->WRITE("code",0);
				$this->WRITE("msg","编辑文章失败！");
				$this->RESPONSE();
			}
		}
		
	}
	

 

	
	

}